package com.qixin.api.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qixin.api.bean.*;
import com.qixin.api.mapper.ComPeopleMapper;
import com.qixin.api.service.ComPeopleService;
import com.qixin.api.service.ComPtUserService;
import com.qixin.util.cache.EhcacheUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 薛向毅
 * @since 2018-09-28
 */
@Service
public class ComPeopleServiceImpl extends ServiceImpl<ComPeopleMapper, ComPeople> implements ComPeopleService {
	private Logger logger = LoggerFactory.getLogger(ComPeopleServiceImpl.class);
    @Autowired
    ComPtUserService comPtUserService;



    @Override
    public ResultPage<ComPeople> selectMyPage(VoPage voPage, HttpSession session) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("userid", ((ComPtUser) session.getAttribute("user")).getId());
        List<ComPeople> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.selectMyCount(paramMap);
        ResultPage<ComPeople> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public ResultPage<ComPeople> selectALL(VoPage voPage) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<ComPeople> list = baseMapper.selectALL(paramMap);
        Integer count = baseMapper.countMyPageq(paramMap);
        ResultPage<ComPeople> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public ResultPage<ComPeople> selectphone(VoPage voPage, Long phone) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("phone", phone);
        List<ComPeople> list = baseMapper.selectphone(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<ComPeople> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }


    /**
     * 张子艺
     **/
    @Override
    public List<ZtreeBean> findPtZtree(String ptName, Long id) {
        return baseMapper.findPtZtree(ptName, id);
    }

    /**
     * 张子艺
     **/
    @Override
    public ResultPage<ComPeople> PeopleList(VoPage voPage, Long id,Long ptId,String peopleName) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("peopleName", peopleName);
        //树双击带参查询列表
        if(ptId!=null){
            id=ptId;
        }
        List<ComPeople> list = baseMapper.selectMyPageForPt(paramMap, id);
        Integer count = baseMapper.countMyPageForPt(paramMap, id);
        ResultPage<ComPeople> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    /**
     * 张子艺
     **/
    @Override
    public String addPeople(ComPeople comPeople, Long ptId) {
        Map map = new HashMap<>();
        map.put("phone", comPeople.getPhone());
        map.put("name", comPeople.getName());
        List<ComPeople> comPeoples = this.selectByMap(map);
        if (comPeoples.size() > 0) {
            comPeople = comPeoples.get(0);
            if (comPeople.getFlag().equals("2")) {
                return "该用户还未通过审核";
            } else {
                if(comPeople.getUserId()!=0){
                    return "该员工已是其他企业员工，请先从其他企业中解除关系，再进行添加。";
                }

                comPeople.setUserId(ptId);

                if (this.updateById(comPeople)) {
                    return "添加成功";
                }
            }
        } else {
            return "该用户还未注册过，请先注册该用户";
        }
        return "服务器出错，请稍后再试";
    }

    /**
     * 张子艺
     **/
    @Override
    public boolean removeMoenyForWeChat(Long userId, BigDecimal moeny) {
        if (baseMapper.removeMoenyForWeChat(userId, moeny) > 0) {
            return true;
        } else {
            return false;
        }
    }

	@Override
	public String updatePeople(String param, String code) {
		ComPeople comPeople=  JSONObject.parseObject(param,ComPeople.class);
		if (!equalsCache(code,comPeople.getPhone()+"")){
			return  "验证码错误";

		}
		comPeople.setUserName(comPeople.getPhone()+"");
		comPeople.setPassWord(comPeople.getPassWord());
		if (baseMapper.updatePeople(comPeople)>=0){
			return  "修改成功";
		}
     else {
      	return "无该个人账号";
	  }

	}


	public boolean equalsCache(String code,String phone) {

		//从缓存中获取六位验证码 当访问过一次后 60秒内没有再此访问会过期  缓存区的key设置成数字加字符串 否则获取不到
		String cacheCode= EhcacheUtil.get("SMS",phone+"Data")+"";
		logger.info("缓存中的验证码："+cacheCode);
		logger.info("用户输入的验证码："+code);
		logger.info("用户手机号："+phone);
		if (code.equals(cacheCode)){
			return true;
		}
		return false;
	}
	@Override
    public String phonea( Long id) {

       String list = baseMapper.phonea(id);

        return list;
    }
    @Override
    public String phoneb( Long id) {

        String list = baseMapper.phoneb(id);

        return list;
    }
}
